package com.tianzeculture.stagebao.activity.business;

import android.content.Intent;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.base.BaseActivity;
import com.tianzeculture.stagebao.bean.BusinessHomepageBean;
import com.tianzeculture.stagebao.net.ArtistRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;
import io.rong.imlib.IFwLogCallback;

import static com.tianzeculture.stagebao.net.base.AppBaseRestUsageV2.PICTURE_URL;

/**
 * Created by Administrator on 2018/8/29.
 */

public class BusinessHomepagerActivity extends BaseActivity {
    @BindView(R.id.iv_header_bg_ext)
    ImageView ivHeaderBgExt;
    @BindView(R.id.iv_header_bg)
    ImageView mIvHeaderBg;
    @BindView(R.id.iv_pi_header)
    CircleImageView ivPiHeader;
    @BindView(R.id.rl_header)
    RelativeLayout rlHeader;
    @BindView(R.id.rl_avatar)
    RelativeLayout mRlavatar;
    //    @BindView(R.id.toolbar)
//    Toolbar toolbar;
    @BindView(R.id.checkAttention)
    CheckBox checkAttention;
    //    @BindView(R.id.title)
//    RelativeLayout titleLayout;
//    @BindView(R.id.titleTransparent)
//    RelativeLayout transparentTitle;
//    @BindView(R.id.collapsing_toolbar)
//    CollapsingToolbarLayout collapsingToolbar;
//    @BindView(R.id.appBarlayout)
//    AppBarLayout mAppBarLayout;
    @BindView(R.id.tvArtistName)
    TextView tvArtistName;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    //    @BindView(R.id.toolbar_title)
//    TextView toolbarTitle;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    //    @BindView(R.id.tv_profession)
//    TextView tvProfession;
    @BindView(R.id.tvClick)
    TextView tvClick;
    @BindView(R.id.tvContent)
    TextView tvContent;
    @BindView(R.id.mRecyclerview)
    RecyclerView mRecyclerview;
    private static final int CDESC_CODE = 111;
    public static final int ADAPTER_CODE = 222;

    @Request
    ArtistRestUsage artistRestUsage = null;

    private int toWorkerId = 1;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_business_homepager;
    }

    @Override
    public void initUI() {
        super.initUI();
//        initToolbar();
//        initRefresh();
        tvClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(BusinessHomepagerActivity.this, WorkBriefActivity.class);
                startActivityForResult(intent, CDESC_CODE);
            }
        });
        Map<String, String> params = new HashMap<>();
        artistRestUsage.toWorker(toWorkerId, params);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Map<String, String> params = new HashMap<>();
        artistRestUsage.toWorker(toWorkerId, params);
    }

//    private void initToolbar() {

//        toolbar.setNavigationIcon(R.mipmap.icon_return_white);
//        collapsingToolbar.setContentScrimColor(getResources().getColor(R.color.colorGray_F8));
////        //因为我暂时没有找到比较好的透明状态栏来适配这一套效果布局。
//        getWindow().setStatusBarColor(getResources().getColor(R.color.colorGray_F8));
    // 因此就直接替换掉StatusBar的颜色，这样其实也蛮好看的。

//        collapsingToolbar.setTitle("我的主页");
//        tvTitle.setText("我的主页");
//        toolbar.setTitle("");
//        setSupportActionBar(toolbar);
//        int statusHeight = getStatusHeight(this);
//
//        toolbar.measure(0, 0);
//        int measuredHeight = toolbar.getMeasuredHeight();
//
//        ViewGroup.LayoutParams layoutParams = toolbar.getLayoutParams();
//        layoutParams.height = measuredHeight + statusHeight;
//        toolbar.setLayoutParams(layoutParams);
//
//
//        ViewGroup.LayoutParams params = titleLayout.getLayoutParams();
//        params.height = measuredHeight + statusHeight;
//        titleLayout.setLayoutParams(params);
//
//        ViewGroup.LayoutParams params2 = transparentTitle.getLayoutParams();
//        params2.height = measuredHeight + statusHeight;
//        transparentTitle.setLayoutParams(params2);
//    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CDESC_CODE) {
            if (data != null) {
                tvContent.setText(data.getExtras().getString("cdesc"));
            }
        }
        if (requestCode == ADAPTER_CODE){
            Map<String, String> params = new HashMap<>();
            artistRestUsage.toWorker(toWorkerId, params);
        }
    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == toWorkerId) {
            if (msg.getData()) {
                BusinessHomepageBean bean = (BusinessHomepageBean) msg.getObj();
                refreshData(bean);
            }
        }
    }


    private void refreshData(BusinessHomepageBean bean) {
        tvArtistName.setText(bean.getUser().getNickname());
        tvAddress.setText(bean.getUser().getAddress());
        tvContent.setText(bean.getUser().getCdesc());
        Glide.with(this).load(PICTURE_URL + bean.getUser().getImgurl()).error(R.mipmap.icon_head).into(ivPiHeader);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerview.setLayoutManager(manager);
        BusinessHomeAdapter adapter = new BusinessHomeAdapter(R.layout.item_business_homepage, bean.getList(), this);
        mRecyclerview.setAdapter(adapter);
    }

//    private void initRefresh() {
//        mIvHeaderBg.setScaleX(1.3f);
//        mIvHeaderBg.setScaleY(1.3f);
//
//        final int[] location = new int[2];
//        ivPiHeader.getLocationInWindow(location);
//        final int endOffset = DensityUtil.dip2px(this, 200);
//
//        final int[] pointV = new int[2];
//        tvArtistName.getLocationInWindow(pointV);
//        mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
//            @Override
//            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
//                int[] ponitF = new int[2];
//                int disten = -verticalOffset;
//                int disOffset = endOffset;
//                if (disten >= disOffset) {
//                    mRlavatar.setAlpha(0);
//                    mIvHeaderBg.setScaleX(1.0f);
//                    mIvHeaderBg.setScaleY(1.0f);
//                    titleLayout.setVisibility(View.VISIBLE);
//                    transparentTitle.setVisibility(View.GONE);
//                } else if (disten == 0) {
//                    mRlavatar.setAlpha(1);
//                    mIvHeaderBg.setScaleX(1.3f);
//                    mIvHeaderBg.setScaleY(1.3f);
//                } else {
//                    float precents = (float) (disOffset - disten) / disOffset;
//                    mRlavatar.setAlpha(precents);
//                    mIvHeaderBg.setScaleX(1.0f + 0.3f * (precents));
//                    mIvHeaderBg.setScaleY(1.0f + 0.3f * (precents));
//                    titleLayout.setVisibility(View.GONE);
//                    transparentTitle.setVisibility(View.VISIBLE);
//                }
//
//                ivPiHeader.getLocationInWindow(ponitF);
//                int y = ponitF[1];
//                if (y > 0) {
//                    if (y < endOffset) {
////                        titleLayout.setVisibility(View.GONE);
////                        transparentTitle.setVisibility(View.VISIBLE);
////                        toolbar.setBackgroundResource(R.color.colorTransparent);
//                        float precent = (float) (endOffset - y) / endOffset;
//                        toolbar.setAlpha(precent);
//                    } else if (y >= endOffset) {
//                        toolbar.setAlpha(0);
////                        toolbar.setBackgroundResource(R.color.colorGray_F0);
////                        titleLayout.setVisibility(View.GONE);
////                        transparentTitle.setVisibility(View.VISIBLE);
//                    }
//                } else if (y < 0) {
//                    toolbar.setAlpha(1);
////                    toolbar.setBackgroundResource(R.color.colorGray_F0);
////                    titleLayout.setVisibility(View.VISIBLE);


////                    transparentTitle.setVisibility(View.GONE);
//                } else if (y == 0) {
////                    titleLayout.setVisibility(View.VISIBLE);
////                    transparentTitle.setVisibility(View.GONE);
////                    toolbar.setBackgroundResource(R.color.colorTransparent);
//                    toolbar.setAlpha(1);
//                }
//            }
//        });
//    }
}
